package com.aplink.generic.xmlparser;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.text.TextUtils;

import com.aplink.generic.constant.PageCollectionConstant;
import com.aplink.generic.pojo.PageCollection;
import com.aplink.generic.pojo.PageItem;
import com.aplink.generic.pojo.controlspec.menu.PopupMenuSpec;
import com.aplink.generic.util.ResourcesHelper;

public class PageCollectionParser {
    private static String getViewTitile(final String viewTitle) {
        return ResourcesHelper.getStringByName(viewTitle);
    }

    public static PageCollection parse(Node nodeFormat) {
        final PageCollection pageCollection = new PageCollection();
        Element elementFormat = (Element) nodeFormat;
        NodeList nodeListFormat = elementFormat
                .getElementsByTagName(PageCollectionConstant.PAGES);
        if (nodeListFormat.getLength() == 0) {
            return pageCollection;
        }
        nodeFormat = nodeListFormat.item(0);
        if (nodeFormat.getNodeType() == Node.ELEMENT_NODE) {
            if (nodeFormat.hasAttributes()) {
                final String index = XMLPaserUtil.getAttrValue(nodeFormat,
                        PageCollectionConstant.INDEX);
                if (!TextUtils.isEmpty(index)) {
                    pageCollection.setIndex(Integer.parseInt(index));
                }
            }
            elementFormat = (Element) nodeFormat;
            nodeListFormat = elementFormat
                    .getElementsByTagName(PageCollectionConstant.PAGE);
            for (int i = 0; i < nodeListFormat.getLength(); i++) {
                final Node node = nodeListFormat.item(i);
                pageCollection.add(PageCollectionParser.parsePageItem(node));
            }
        }
        return pageCollection;
    }

    private static PageItem parsePageItem(final Node node) {
        final PageItem pageItem = new PageItem();
        if (node.hasAttributes()) {
            final String formDesign = XMLPaserUtil.getAttrValue(node,
                    PageCollectionConstant.FORM_DESIGN);
            if (!TextUtils.isEmpty(formDesign)) {
                pageItem.setFormDesign(formDesign);
            }
            final String contentData = XMLPaserUtil.getAttrValue(node,
                    PageCollectionConstant.CONTENT_DATA);
            if (!TextUtils.isEmpty(contentData)) {
                pageItem.setContentData(contentData);
            }
            final String title = XMLPaserUtil.getAttrValue(node,
                    PageCollectionConstant.TITLE);
            if (!TextUtils.isEmpty(title)) {
                pageItem.setTitle(PageCollectionParser.getViewTitile(title));
            }
            final String name = XMLPaserUtil.getAttrValue(node,
                    PageCollectionConstant.NAME);
            if (!TextUtils.isEmpty(name)) {
                pageItem.setName(name);
            }

            final String icon = XMLPaserUtil.getAttrValue(node,
                    PageCollectionConstant.ICON);
            if (!TextUtils.isEmpty(icon)) {
                pageItem.setIcon(icon);
            }
        }
        final PopupMenuSpec popupMenuFormat = PopupMenuParser
                .parsePopupMenuFormat(node);
        pageItem.setPopupMenuSpec(popupMenuFormat);
        return pageItem;
    }
}
